//1.按需导入模块
//@符号 脚手架配置提供的别名 表示项目的src目录
import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/locaData'

//2.注册路由组件  可以用到<router-link> <router-view>

Vue.use(VueRouter)
 
//3.路由映射关系
import Login from '@/views/Login.vue'
import Home from '@/views/Home.vue'
import Index from '@/views/Index.vue'
import PostList from '@/views/Post/List.vue'
import PosPubLish from '@/views/Post/Publish.vue'
const routes = [
    { path: '/login', component: Login },
    {path:'/',redirect:'/home' },
    {
        path: '/home', component: Home,
        redirect: '/home/index',
      //子路由不写 /
        children:[
        { path: 'index', component:Index },
        { path: 'post_list', component:PostList },
        { path: 'post_publish', component:PosPubLish }
        ]}
]

//4.创建路由实例
const router= new VueRouter({
    routes
})

//from 来自哪里
//to 去到那里
//next 是否放行的函数

//前置导航守卫
router.beforeEach((to, from, next) => {
    //后台管理系统,只有登录是直接放行
    if (to.path ==='/login'){
     next()
    }else {
    //其他都要检查
        if (getToken()) {
           next()
        } else {
            // next({path:'/login'})
            next('/login')
       }
    }
})
export default router